import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:xsdk_flutter/xsdk_flutter_method_channel.dart';


class SplashAd {
  int objectId = 0;
  late MethodChannel methodChannel;

  void Function(String)? onLoad;
  void Function()? onDismissed;
  void Function(String)? onError;
  void Function()? onClicked;

  onCreateEvent(Map data) {
    if (data.containsKey("method")) {
      var method = data["method"] as String;
      if ("onLoad".compareTo(method) == 0) {
        if (onLoad != null) {
          onLoad!(data["ret"]);
        }
      } else if ("onClicked".compareTo(method) == 0) {
        if (onClicked != null) {
          onClicked!();
        }
      } else if ("onError".compareTo(method) == 0) {
        if (onError != null) {
          onError!(data["ret"]);
        }
      } else if ("onDismissed".compareTo(method) == 0) {
        if (onDismissed != null) {
          onDismissed!();
        }
      }
    }
  }

  SplashAd(
      {required this.objectId,
      required this.methodChannel,
      this.onLoad,
      this.onDismissed,
      this.onClicked,
      this.onError});

  void  show({String logoUrl = ""}) async{
    await methodChannel.invokeMethod<int?>(
        'showSplashAd', jsonEncode({"objectId": objectId, "params": logoUrl}));
  }

  void destory() async {
    await methodChannel.invokeMethod<int?>(
        'destorySplashAd', jsonEncode({"objectId": objectId}));
    MethodChannelXsdkFlutter.bannerAdMap.remove(objectId);
  }
}